package com.koron.css2.workflow;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Map;

/**
 * 流程平台单点地址
 * @author admin
 * @version 2.0
 * @since 2021-06-09
 *
 */
public class ProcessPlatformSSO implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(ProcessPlatformSSO.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", String.class);
		try {
			Map<String, String> param = JsonUtils.objectToPojo(req.getData(), Map.class);
			if(StringUtils.isEmpty(param.get("processCode"))) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "processCode 参数不能为空", String.class);
			}
			if(StringUtils.isEmpty(param.get("setCode"))) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "setCode 参数不能为空", String.class);
			}
			String url = ApplicationConfig.getWorkflowSSOUrl();
			url = url + "?secret=" + WorkflowImpl.getSecret() + "&processCode=" + param.get("processCode") + "&setCode=" 
			+ param.get("setCode") + "&account=" + userInfo.getUserInfo().getAcount() + "&appCode=" + ApplicationConfig.getWorkflowAppCode();
			info.setData(url);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			log.error(e.getMessage(), e);
		}
		return info;
	}
}
